﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;

namespace ERP.View
{
    public partial class viewListThuoctinh : DevExpress.XtraEditors.XtraForm
    {
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public int flag = 0;
        public int count = 0;
        public cs_erp_hh_thuoctinhInfo hh = new cs_erp_hh_thuoctinhInfo();

        public viewListThuoctinh()
        {
            InitializeComponent();
        }

        private void btnMoi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            viewErp_Thuoctinh hh = new viewErp_Thuoctinh();
            hh.flag = 1;
            hh.user = this.user;
            hh.ShowDialog();
            this.Load_Data();
        }

        private void btnDieuchinh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hh.id_thuoctinh = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                viewErp_Thuoctinh hh = new viewErp_Thuoctinh();
                hh.flag = 2;
                hh.hh = this.hh;
                hh.user = this.user;
                hh.ShowDialog();
                this.Load_Data();
            }
        }

        private void btnThoat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.Dispose(true);
        }

        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hh.id_thuoctinh = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                DialogResult dg = new DialogResult();
                dg = MessageBox.Show("Bạn muốn xóa thuộc tính: " + this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcTenkv).ToString().Trim() + "?", "Cảnh báo", MessageBoxButtons.YesNo);
                if (dg == DialogResult.Yes)
                {
                    this.hh.ExcuteProcedure(3);
                    this.Load_Data();
                }
            }
        }

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");

            ds = this.hh.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
            }
        }

        private void viewListThuoctinh_Load(object sender, EventArgs e)
        {
            this.Load_Data();
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnMoi.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                    this.btnXoa.Enabled = false;
                }
                if (userright.updatedata == 0)
                {
                    this.btnDieuchinh.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }

        private void grcDanhsach_DoubleClick(object sender, EventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hh.id_thuoctinh = int.Parse(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, grdcID).ToString().Trim());
                viewErp_Thuoctinh hh = new viewErp_Thuoctinh();
                hh.flag = 2;
                hh.hh = this.hh;
                hh.user = this.user;
                hh.ShowDialog();
                this.Load_Data();
            }
        }
    }
}